<html>
<head><meta charset="utf-8"><title>Pin usage · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20usage.html">Pin usage</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212825552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20usage/near/212825552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> keegan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20usage.html#212825552">(Oct 09 2020 at 13:31)</a>:</h4>
<p>Looking to do something like this. This way I can maintain a parsed representation of <code>source</code> while remaining zero-copy. The problem is that I am not sure how to do this safely. I believe Pin is the way to go here, but I do not really understand Pin or its usage here. The documentation has a section on self-referential structs but I am still struggling. Here's the code:</p>
<div class="codehilite"><pre><span></span><code>struct Stuff&lt;&#39;a, T&gt; {
    pub(crate) source: T,
    inner: StuffInner&lt;&#39;a&gt;, // Points into `source`
}

pub fn from_path&lt;&#39;a, P: AsRef&lt;Path&gt;&gt;(path: P) -&gt; Result&lt;Stuff&lt;&#39;a, Mmap&gt;&gt; {
    let source = unsafe { MmapOptions::new().map(&amp;File::open(path.as_ref())?)? };
    let inner = source.pread_with::&lt;StuffInner&gt;(0, scroll::LE)?;
    Ok(Stuff {
        source, // Moves `source` into `Stuff`
        inner // But `inner` is borrowing `source` as it points into it
    })
}
</code></pre></div>


<p>Is this is a good case for Pin, if so how can I use it? Will it require <code>unsafe</code> code or can I use something like <code>pin_project</code> for this? Many thanks.</p>



<a name="212880843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20usage/near/212880843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20usage.html#212880843">(Oct 09 2020 at 22:09)</a>:</h4>
<p>You might want to ask on <a href="http://users.rust-lang.org">users.rust-lang.org</a>. This is a chat server for <em>development</em> of Rust, not usage.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>